<template>
    <Select v-model="dictValue"
            :clearable="clearable"
            :multiple='multiple'
            :filterable='filterable'
            :placeholder='placeholder'
            :style="{width:width+'px'}"
            @on-change="changeDict"
            :disabled="disabled">
        <Option v-for="item in showOptions" :value="item.value" :key="item.value">{{item.label}}</Option>
    </Select>
</template>

<script>
    import {postAction} from '@/api/request'

    export default {
        name: 'dictSelect',
        props: {
            value: {
                type: String,
                default: ''
            },
            disabled: {
                type: Boolean,
                default: false
            },
            dictCode: {
                type: String,
                default: ''
            },
            showKeys: {
                type: String,
                default: ''
            },
            hiddenKeys: {
                type: String,
                default: ''
            },
            multiple: { //是否支持多选
                type: Boolean,
                default: false
            },
            filterable: { //是否支持搜索,多选搜索时，可以使用键盘Delete快捷删除最后一个已选项
                type: Boolean,
                default: false
            },
            clearable: {//是否可以清空选项，只在单选时有效
                type: Boolean,
                default: true
            },
            dicType: { // code、table、service，code时取sys_dict，table时取表，service时直接调用接口
                type: String,
                default: 'code'
            },
            dictTable: String, //字符串格式，英文逗号分隔，数据库标识(可选，如果不填默认为业务库),表名,字段码,字段明文， 例：subsistence(可选),ss01001,azaa0001,azaa0002
            dictService: String,
            serviceParam: { //dictService的入参
                type: Object,
                default: function () {
                    return new Object();
                }
            },
            placeholder: {
                type: String,
                default: '请选择'
            },
            width: {
                type: Number,
                default: 200
            }
        },
        data() {
            return {
                optionList: [],
                dictValue: this.value,
                dictList: [],
                showOptions: [],
            };
        },
        created() {
            if (this.multiple) {
                //多选string转array
                this.dictValue = this.dictValue.split(',');
            }
            this.initDictData();
        },
        model: {
            prop: 'value',
            event: 'change'
        },
        watch: {
            value: function () {
                if (this.multiple) {
                    //多选string转array
                    this.dictValue = this.value.split(',');
                } else {
                    this.dictValue = this.value;
                }
                this.initDictData()
            }
        },
        methods: {
            //获取数据
            initDictData() {
                if (this.dicType == 'table') {
                    this.getTableDictList();
                } else if (this.dicType == 'service') {
                    this.getDictService();
                } else {
                    this.getDictList();
                }
            },
            //获取表字段转义方式字典数据
            getTableDictList() {
                var that = this;
                if (that.dictTable) {
                    postAction('/api/ac/subsistence/extSysDictService/getTableDictList', {keywords: that.dictTable}).then((res) => {
                        if (res.data.errcode == 0) {
                            that.optionList = res.data.data.results;
                            that.handleValueShow();
                        }
                    })
                }
            },
            //接口形式获取数据
            getDictService() {
                var that = this;
                if (that.dictService) {
                    postAction(that.dictService, that.serviceParam).then((res) => {
                        if (res.data.errcode == 0) {
                            that.optionList = res.data.data.results;
                            that.handleValueShow();
                        }
                    })
                }
            },
            //获取字典信息
            getDictList() {
                var that = this;
                if (sessionStorage.getItem("sys_dict_list")) {
                    this.dictList = JSON.parse(sessionStorage.getItem("sys_dict_list"));
                    that.getDictListByCode();
                } else {
                    postAction('/api/ac/subsistence/extSysDictService/getDictList', {}).then((res) => {
                        if (res.data.errcode == 0) {
                            that.dictList = res.data.data;
                            sessionStorage.setItem("sys_dict_list", JSON.stringify(that.dictList));
                            that.getDictListByCode();
                        }
                    })
                }
            },
            //根据字典code获取字典列表
            getDictListByCode() {
                if (this.dictCode) {
                    var dictList = this.dictList;
                    var optionList = [];
                    for (var i in dictList) {
                        if (dictList[i].type == this.dictCode) {
                            optionList.push(dictList[i]);
                        }
                    }
                    this.optionList = optionList;
                }
                this.handleValueShow();
            },
            handleValueShow() {
                var showOptions = [];
                if (this.showKeys) {
                    var showKeys = this.showKeys.split(",");
                    var optionList = this.optionList;
                    for (var i in optionList) {
                        if (showKeys.indexOf(optionList[i].value) > -1) {
                            showOptions.push(optionList[i])
                        }
                    }
                } else if (this.hiddenKeys) {
                    var hiddenKeys = this.hiddenKeys.split(",");
                    var optionList = this.optionList;
                    for (var i in optionList) {
                        if (hiddenKeys.indexOf(optionList[i].value) <= -1) {
                            showOptions.push(optionList[i])
                        }
                    }
                }
                else {
                    showOptions = this.optionList;
                }
                this.showOptions = showOptions;
            },
            changeDict(e) {
                if (this.multiple) {
                    //多选操作array转string
                    e = e.toString();
                }
                this.$emit('change', e)
            }
        },
    }
</script>
<style scoped>
</style>
